import {
  defineStore
} from 'pinia'

export const userStore = defineStore('menu', {
  state: () => {
    return {
      menuList: []
    }
  },
  actions: {
    setMenuList(menuList) {
      this.menuList = menuList
    }
  },
  // 开启持久化
  persist: {
    enabled: true, // 启用
    strategies: [
      // storage 可选localStorage或sessionStorage
      // paths 给指定数据持久化
      {
        key: 'menu',
        storage: localStorage,
        paths: ['menuList']
      }
    ]
  },
  getters: {
    getMenuList(state) {
      return state.menuList
    }
  }
})

const instance = userStore()
instance.$subscribe((_, state) => {
  localStorage.setItem('BI_menus', JSON.stringify({ ...state }))
})
const old = localStorage.getItem('BI_menus');
if (old) {
  instance.$state = JSON.parse(old);
}